package view;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JList;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JTextField;
import org.jdom.JDOMException;
import util.PersistenciaXml;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import language.Strings;

public class ConfigColeta extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JList jList = null;
	private JButton btSalvar = null;
	private JButton btCancelar = null;
	private JButton btRemover = null;
	private JTextField jTextField = null;
	private JButton btAdd = null;

	public ConfigColeta() {
		super();
		initialize();
		
		carregaDados();
	}

	private void initialize() {
		this.setSize(385, 342);
		this.setContentPane(getJContentPane());
		this.setTitle(Strings.TITULO_COLETA_DADOS);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setResizable(false);
		this.setLocation(
						((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (this
								.getWidth() / 2)),
						((Toolkit.getDefaultToolkit().getScreenSize().height / 2) - (this
								.getHeight() / 2)));
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
				Principal.JANELA_ABERTA = false;				
			}			
		});
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJList(), null);
			jContentPane.add(getBtSalvar(), null);
			jContentPane.add(getBtCancelar(), null);
			jContentPane.add(getBtRemover(), null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(getBtAdd(), null);
		}
		return jContentPane;
	}

	private JList getJList() {
		if (jList == null) {
			jList = new JList();
			jList.setBounds(new Rectangle(17, 150, 190, 96));
		}
		return jList;
	}

	private JButton getBtSalvar() {
		if (btSalvar == null) {
			btSalvar = new JButton();
			btSalvar.setBounds(new Rectangle(55, 270, 110, 25));
			btSalvar.setText(Strings.TEXT_BT_SALVAR);
			btSalvar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					DefaultListModel model = (DefaultListModel) getJList().getModel();
					List<String> listSeriais = new ArrayList<String>();
															
					for (int i = 0; i < model.size(); i++) {
						listSeriais.add(model.elementAt(i).toString());
					}
					
					PersistenciaXml persistenciaXml = new PersistenciaXml();
					persistenciaXml.salvaSeriais(listSeriais);
				}
			});
		}
		return btSalvar;
	}

	private JButton getBtCancelar() {
		if (btCancelar == null) {
			btCancelar = new JButton();
			btCancelar.setBounds(new Rectangle(225, 270, 110, 25));
			btCancelar.setText(Strings.TEXT_BT_CANCELAR);
			btCancelar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					carregaDados();
				}
			});
		}
		return btCancelar;
	}

	private JButton getBtRemover() {
		if (btRemover == null) {
			btRemover = new JButton();
			btRemover.setBounds(new Rectangle(235, 150, 100, 25));
			btRemover.setText(Strings.TEXT_BT_REMOVER);
			btRemover.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					DefaultListModel model = (DefaultListModel) getJList().getModel();
					
					if(getJList().isSelectionEmpty()){
						JOptionPane.showMessageDialog(null, Strings.TEXT_LABEL_SELECIONE_ITEM_LISTA);
						return;
					}
					
					model.remove(getJList().getSelectedIndex());
					getJList().repaint();
					getJList().validate();
				}
			});
		}
		return btRemover;
	}

	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(17, 100, 190, 19));
		}
		return jTextField;
	}

	private JButton getBtAdd() {
		if (btAdd == null) {
			btAdd = new JButton();
			btAdd.setBounds(new Rectangle(235, 100, 100, 25));
			btAdd.setText("Add");
			btAdd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {					
					DefaultListModel model = (DefaultListModel) getJList().getModel();
					
					if(getJTextField().getText().equals("")){
						JOptionPane.showMessageDialog(null, Strings.TEXT_LABEL_CAMINHO_INVALIDO);
						return;
					}
					
					model.addElement(getJTextField().getText());
					getJList().repaint();
					getJList().validate();
					
					getJTextField().setText("");
				}
			});
		}
		return btAdd;
	}
	
	private void carregaDados(){
		PersistenciaXml persistenciaXml = new PersistenciaXml();
		
		try {
			List<String> listSeriais = persistenciaXml.selectSeriaisXML();
			
			DefaultListModel model = new DefaultListModel();
			getJList().setModel(model);
						
			for (int i = 0; i < listSeriais.size(); i++) {
				model.add(i, listSeriais.get(i));
//				model.addElement(listSeriais.get(i));
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
